<!--
 * @Author: wrh123 482597539@qq.com
 * @Date: 2024-01-11 14:41:45
 * @LastEditors: wrh123 482597539@qq.com
 * @LastEditTime: 2024-01-22 22:10:21
 * @FilePath: /vue-admin/src/views/staff/components/staffadd.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 对话框 -->
  <div>
    <el-dialog :title="'添加员工'" :visible="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
        <!-- <el-form-item prop="username" label="账号">
          <el-input  v-model="ruleForm.username" size="mini" placeholder="请输入账号" />
        </el-form-item> -->
        <el-form-item prop="username" label="员工姓名">
          <el-input v-model="ruleForm.username" size="mini" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="ruleForm.phone" size="mini" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="ruleForm.password" size="mini" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="department" label="所属部门">
          <el-select v-model="ruleForm.department" placeholder="请选择部门" size="mini">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="ruleForm.id" label="禁用">
          <el-radio-group v-model="status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="end" style="margin-top: 10px;">
            <el-button size="mini" @click="handleClose">取 消</el-button>
            <el-button size="mini" type="primary" @click="btnOK">确 定</el-button>
          </el-row>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>

</template>

<script>
import { addStaff, updateStaff, getStaffPageEdit } from '@/api/staff'

export default {
  name: 'VueAdminStaffadd',
  props: {
    dialogVisible: {
      default: false,
      type: Boolean
    },
    currid: {
      default: null,
      type: Number
    },
  },
  data() {
    return {
      showVisible: false,
      ruleForm: {
        password: null,
        phone: null,
        sex: null,
        username: null,
        id: null,
        department: null, // 员工所属的部门
      },
      options: [
        { id: 1, name: '财务' },
        { id: 2, name: '客服' },
        { id: 3, name: '人事' },
        { id: 4, name: '风控' },
      ],
      title: null,
      status: 0,
      rules: {
        username: [
          { required: true, message: '姓名不可以为空', trigger: ['blur', 'change'] }],
        password: [
          { required: true, message: '密码不可以为空', trigger: ['blur', 'change'] }],
        department: [
          { required: true, message: '所属部门不可以为空', trigger: ['blur', 'change'] }],
        phone: [
          { required: true, message: '手机号不可以为空', trigger: ['blur', 'change'] },
          {
            required: true,
            pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],

        sex: [
          { required: true, message: '性别不可以为空', trigger: ['change'] }],
      }
    }
  },
  watch: {
    currid(val) {
      this.ruleForm.id = val
    }
  },
  mounted() {

  },

  methods: {
    // 取消
    handleClose() {
      this.ruleForm = {
        password: '',
        phone: '',
        sex: 0,
        username: ''
      }
      this.status = 0
      this.$refs.ruleForm.resetFields()
      this.$emit('update:dialogVisible', false)
    },

    // 确定
    async btnOK() {
      // console.log('this.ruleForm.id',this.ruleForm.id);
      await this.$refs.ruleForm.validate()
      if (this.ruleForm.id) {
        this.showVisible = true
      } else {
        await addStaff(this.ruleForm)
        this.$message.success('添加成功')
        // this.emit('getStaffList')
        this.$emit('getStaffList')
        this.handleClose()
      }
    },
    // // 获取详情
    // async getDetail(row) {
    //   // console.log('row',row);
    //   if (!row) return this.$message.error('参数错误')
    //   // this.ruleForm = { ...row }
    //   const res = await getStaffPageEdit(row)
    //   // console.log(res,'res');
    //   // this.ruleForm = res
    //   this.ruleForm = {...res, id: row}
    // },
    // 编辑取消
    Close() {
      this.showVisible = false
      this.handleClose()
    },
    // computed:{
    //   getUser(){
    //     const titleName = ''
    //     titleName =  this.ruleForm.id ? '编辑员工':'添加员工'
    //     return  titleName
    //   }
    // },
    async delBtnOK() {
      await updateStaff({ ...this.ruleForm, status: this.status })
      this.$message.success('修改成功')
      this.$emit('getStaffList')
      // 确定修改
      this.Close()
    }
  }
}
</script>

<style lang="scss" scoped></style>
